<?php /* УТФ-8:fix */

class SPIcontroller__media__image extends SPIcontroller__media
{
    public function build( $media_source, $cache_source, $attrs = null )
    {
        if( ini_set( 'memory_limit', '100M' ) === false )
        {
            trigger_error( "Cannot change memory limit to '100M'", E_USER_NOTICE );
        }

        //---------------------------------------------------------------------

        if( is_array( $attrs ) === false )
        {
            $attrs = array();
        }

        $attrs['fixed_width']   = (int) $attrs['fixed_width'];
        $attrs['fixed_height']  = (int) $attrs['fixed_height'];

        switch( $attrs['cache_type'] )
        {
            default:
            {
                $attrs['cache_type'] = 'jpg';
            }
            case 'jpg':
            {
                $attrs['cache_quality'] = (int) $attrs['cache_quality'];

                if( $attrs['cache_quality'] == 0 )
                {
                    $attrs['cache_quality'] = 95;
                }
                break;
            }
        }

        //---------------------------------------------------------------------

        list( $width, $height, $imagetype ) = $imageinfo = getimagesize( $media_source );

        switch( $imagetype )
        {
            case IMAGETYPE_GIF:
            {
                $in_image = imagecreatefromgif( $media_source );
                break;
            }

            case IMAGETYPE_JPEG:
            {
                $in_image = imagecreatefromjpeg( $media_source );
                break;
            }

            case IMAGETYPE_PNG:
            {
                $in_image = imagecreatefrompng( $media_source );
                break;
            }

            case IMAGETYPE_WBMP:
            {
                $in_image = imagecreatefromwbmp( $media_source );
                break;
            }

            default:
            {
                $in_image = imagecreatefromstring( file_get_contents( $media_source ) );
                break;
            }
        }

        if( is_resource( $in_image ) === false )
        {
            trigger_error('Cannot open image ('.$media_source.')');
            return false;
        }

        $in_width  = imagesx( $in_image );
        $in_height = imagesy( $in_image );

        //-------------------------------------------------------------------------
        // Try set width=640 and height->count.

        $new_width  = $attrs['fixed_width'];
        $new_height = $attrs['fixed_height'];

        $is_just_copy = true;

        if( $new_width && $new_height )
        {
            if( $in_width > $new_width || $in_height > $new_height )
            {
                $scale = max( $new_width/$in_width, $new_height/$in_height );

                $new_left   = 0;
                $new_top    = 0;
                #new_width  is defined
                #new_height is defined

                $get_width  = ceil( $new_width  / $scale );
                $get_height = ceil( $new_height / $scale );

                $get_left   = floor( ( $in_width  - $get_width  ) / 2 );
                $get_top    = floor( ( $in_height - $get_height ) / 2 );

                $is_just_copy = false;
            }
        }
        elseif( $new_height == 0 )
        {
            if( $new_width < $in_width )
            {
                $new_left   = 0;
                $new_top    = 0;
                #new_width  is defined
                $new_height = ceil( $new_width / $in_width * $in_height );

                $get_left   = 0;
                $get_top    = 0;
                $get_width  = $in_width;
                $get_height = $in_height;

                $is_just_copy = false;
            }
        }
        elseif( $new_width == 0 )
        {
            if( $new_height < $in_height )
            {
                $new_left   = 0;
                $new_top    = 0;
                $new_width  = ceil( $new_height / $in_height * $in_width );
                #new_height is defined

                $get_left   = 0;
                $get_top    = 0;
                $get_width  = $in_width;
                $get_height = $in_height;

                $is_just_copy = false;
            }
        }
        
        if( $is_just_copy )
        {
            // cannot copy because not save content-type :(
            // then -> new-values = in-values

            $new_left   = 0;
            $new_top    = 0;
            $new_width  = $in_width;
            $new_height = $in_height;

            $get_left   = 0;
            $get_top    = 0;
            $get_width  = $in_width;
            $get_height = $in_height;
        }

        //---------------------------------------------------------------------

        $sv_image = imagecreatetruecolor( $new_width, $new_height );

        if( is_resource( $sv_image ) === false )
        {
            trigger_error('Cannot create image');
            return false;
        }

        //-------------------------------------------------------------------------

        // set background color
        imagefill( $sv_image,
            0, 0,
            imagecolorallocate( $sv_image, 255, 255, 255 )
        );

        // resample image
        imagecopyresampled( /*dst*/ $sv_image,
                            /*src*/ $in_image,
                            /*dst*/ $new_left,  $new_top,
                            /*src*/ $get_left,  $get_top,
                            /*dst*/ $new_width, $new_height,
                            /*src*/ $get_width, $get_height
                            );

        //-------------------------------------------------------------------------

        switch( $attrs['cache_type'] )
        {
            case 'jpg':
            {
                imagejpeg( $sv_image, $cache_source, $attrs['cache_quality'] );
                break;
            }
        }

        return true;
    }
}
